<%--
  - Copyright 2011 Angel Sanadinov
  -
  - This file is part of VBox WMI.
  -
  - VBox WMI is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - VBox WMI is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
  --%>

<%@page import="Utilities.ExceptionMessages"%>
<%@page import="Managers.Utilities.RequestResponseWrapper"%>
<%@page import="Managers.Utilities.ResponseMessagesContainer"%>
<%@page import="Utilities.ParameterNames"%>
<%@page import="Managers.Utilities.User"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="jstlc"%>
<%@taglib uri="/WEB-INF/tlds/pagePrintingTags.tld" prefix="p"%>
<%@page contentType="text/html" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%
    //retrieves user data from the session
    User userSessionData = (User)session.getAttribute(ParameterNames.SESSION_USER_OBJECT);
    //retrieves the messages container from the session
    ResponseMessagesContainer messagesContainer =
            (ResponseMessagesContainer)session.getAttribute(ParameterNames.SESSION_REQUEST_MESSAGES_OBJECT);

    //attempts to retrieve a request for message removal
    int removeMessageAtIndex = -1;
    try
    {
        removeMessageAtIndex = Integer.parseInt(request.getParameter("remove"));
    }catch(NumberFormatException e){}

    //removes a message from the containter, if required
    if(removeMessageAtIndex >= 0 && removeMessageAtIndex < messagesContainer.getNumberOfMessages())
    {
        messagesContainer.remove(removeMessageAtIndex);
        response.sendRedirect("errorMessages.jsp");
        return;
    }
    else
        ;
%>
<p:printPageHeader pageName="Error Messages" pageId="pageErrors" userSessionData="<%= userSessionData %>"/>
<jstlc:set var="exceptionCodeMultipleException" value="<%= ExceptionMessages.APP_E_MULTIPLE_EXCEPTIONS %>"/>
<table id="errorsTable" class="resourcesTable">
    <tr><td>Message No.</td><td>Requestor ID</td><td>Server ID</td><td>Request Data</td><td>Response Data</td><td></td><td></td><td></td></tr>
<%
    //goes through every message in the container and prints out the appropriate HTML code
    for(int i = messagesContainer.getNumberOfMessages() - 1; i >= 0; i--)
    {%>
        <jstlc:set var="wrapper" value="<%= messagesContainer.getMessage(i) %>"/>
        <tr ${(wrapper.newMessage) ? "class=\"newErrorMessage\"" : ""} >
            <td><%= i %></td>
            <td>${wrapper.request.requestorId}</td>
            <td>${wrapper.request.serverId}</td>
            <td><a class="actionLink" href="noJavaScript.jsp" onclick="return changePopupVisibility('errorMessageRequest<%= i %>');">Request</a></td>
            <td><a class="actionLink" href="noJavaScript.jsp" onclick="return changePopupVisibility('errorMessageResponse<%= i %>');">Response</a></td>
            <td><a class="actionLink" href="errorMessages.jsp?remove=<%= i %>">Remove</a></td>
            <td><div class="resourceDataTable" id="errorMessageRequest<%= i %>">
                    Message No.: <%= i %><br>
                    ${wrapper.request}
                </div></td>
            <td><div class="resourceDataTable" id="errorMessageResponse<%= i %>">
                    Message No.: <%= i %><br>
                    Request Result: ${wrapper.response.requestResult ? "OK" : "Failed"}<br>
                    Exception Code: ${wrapper.response.exception.exceptionCode}<br>
                    Exception Type: ${wrapper.response.exception.exceptionType}<br>
                    Exception Message: ${wrapper.response.exception.exceptionMessage}<br>
                    <jstlc:if test="${wrapper.response.exception.exceptionCode == exceptionCodeMultipleException}">
                        Additional Exceptions:
                        <a class="actionLink" href="noJavaScript.jsp"
                           onclick="return changePopupVisibility('additionalExceptionsTable<%= i %>', true);">View</a>
                        <table class="resourceDataTable" id="additionalExceptionsTable<%= i %>">
                            <tr><td>No.</td><td>Code</td><td>Type</td><td>Message</td></tr>
                            <jstlc:forEach varStatus="loop" var="exception" items="${wrapper.response.exceptions}">
                                <tr><td>${loop.index}</td>
                                    <td>${exception.exceptionCode}</td>
                                    <td>${exception.exceptionType}</td>
                                    <td>${exception.exceptionMessage}</td></tr>
                            </jstlc:forEach>
                        </table>
                    </jstlc:if>
                </div></td>
        </tr>
        
    <%}
%>
</table>
<script type="text/javascript">
    var errorsPagination = new ElementsTablePagination('errorsTable', 'errorsPagination');
</script>
Page Size: <input type="text" size="2" id="itemsPerPage" value="20">
<a class="pageSizeUpdateLink" href="noJavaScript.jsp"
   onclick="return errorsPagination.setMaxNumberOfItemsPerPage(document.getElementById('itemsPerPage').value);">Update</a>
<p:printPageFooter/>
<% messagesContainer.markAllMessagesAsRead(); %>